<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 * zy_no_log Database of the driver class.
 *
 * @Author:    <mirze@foxmail.com>Mirze
 * @Package:   Database Operate class of the system.
 * @Copyright: Copyright (c) 2005 - 2006 
 * @Version:   $Id: DB_zy_no_log.class.php 2011-07-07 11:43:29 $
 * @Show Note: 数量记录表
 */
/* @Package of the database class */
Class DB_zy_no_log extends PdoMysql
{
    /* Mysql Table Name */
    public $tableName = "zy_no_log";

    /**
     * Construction this Database Object.
     *
     * @Author: Mirze.
     * @Show: Get the PdoMysql Object and Set the default Charset.
     * @Return: ..
     */
    public function __construct()
    {
    		parent::__construct();
    }

    /**
     * Execute the insert method.
     *
     * @Author: Mirze.
     * @Show: Execute 'insert' method by the PdoMysql Object.
     * @Param: $values Array -- Key is the Table Coulnum and Value is that values.
     * @Return: False OR int.
     */
    public function insert_zy_no_log($values)
    {
        return $this->insert($this->tableName, $values);
    }

    /**
     * Execute the update method.
     *
     * @Author: Mirze.
     * @Show: Execute 'update' method by the PdoMysql Object.
     * @Param: $values Array -- Key is the Table Coulnum and Value is that values.
     * @Param: @where  Array -- Key is the Table Coulnum and Value is the where values.
     * @Return: False OR int.
     */
    public function update_zy_no_log($values, $where)
    {
        return $this->update($this->tableName, $values, $where);
    }
    
    /**
     * Execute the delete method.
     *
     * @Author: Mirze.
     * @Show: Execute 'delete' method by the PdoMysql Object.
     * @Param: @where  Array -- Key is the Table Coulnum and Value is the where values.
     * @Return: False OR int.
     */
    public function delete_zy_no_log($where)
    {
        return $this->delete($this->tableName, $where);
    }

    /**
     * Execute the select method.
     *
     * @Author: Mirze
     * @Show: Execute 'select' method by the PdoMysql Object, Return the item set.
     * @Param: $where String -- Search where.
     * @Param: $col String -- Search Order.
     * @Param: $numrows Int -- Search item num.
     * @Param: $offset Int -- Search item set offset.
     * @Param: $field String -- Search item name.
     * @Return: False OR Array.
     */
    public function getAll($where='1', $col='', $numrows=-1, $offset=-1, $field="*")
    {
        $sql = sprintf("SELECT %s FROM %s WHERE %s", $field, $this->tableName, $where);
        return $this->select($sql, $col, $numrows, $offset);
    }

    /**
     * Get one Item.
     *
     * @Author: Mirze
     * @Show: Search the database.Return one item.
     * @Param: $index Array -- Key is the index Key.Value is the index value.
     * @Return: Array.
     */
    public function getOne($mc_log_id)
    {
        $sql = sprintf("SELECT * FROM %s WHERE mc_log_id = '%s'", $this->tableName, $mc_log_id);
        //echo $sql;
        $result = $this->select($sql, 'mc_log_id', 1, 0);
        return isset($result[0]) ? $result[0] : array();
    }

	/**
	 * (功能描述) 更新数量记录表： 条件更新"执行设备记录"
	 *
	 * @param int type 操作类型: 1 验收 2 转资 3 调拨
	 * @return
	 */
	public function updateNoLog($mc_log_id, $type = 1)
	{
		$result = 0;
		$upId = 0;
		$recId = 0;

		$record = $this->getOne($mc_log_id);
		if(empty($record)){
			return $result;
		}
		$sell_no = isset($record['sell_no']) ? intval($record['sell_no']) : 0;
		$arrival_no = isset($record['arrival_no']) ? intval($record['arrival_no']) : 0;
		$auth_no = isset($record['auth_no']) ? intval($record['auth_no']) : 0;
		$transfer_no = isset($record['transfer_no']) ? intval($record['transfer_no']) : 0;
		$allocation_no = isset($record['allocation_no']) ? intval($record['allocation_no']) : 0;
		if(empty($arrival_no)) {
			return $result;
		}

		$subtract = $arrival_no - $auth_no; //验收差值

		switch($type) {
			case '1':
				$where = "mc_log_id = '{$mc_log_id}'"; //通用
				if($subtract > 0) { //更新: +1
					$values = array(
						'auth_no' => $auth_no+1,
					);					
					$upId = $this->update_zy_no_log($values, $where);
				}

				if($upId > 0){
					if($sell_no == $arrival_no) {
						if($subtract == 1 || 0 == $subtract){//已验证完: 更新验收状态
							$db_zml = Factory::getDB('zy_mc_log');
							$val = array('auth_state'=>1);
							$recId = $db_zml->update_zy_mc_log($val, $where);
						}
					}
				}
				if($upId > 0 || $recId > 0) {
					$result = 1;
				}

			    break;
		}
		return $result;
	}
	
	

    
}

/* Public function is the Class method, Design by Hooker framework designer */
/* CreateTime: 2011-07-07 11:43:29 */

?>